/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.pbp.controller;

import java.util.Hashtable;
import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 *
 * @author Agienthea
 */
@Transactional(rollbackFor = Exception.class)
public class PageAccessLevelInterceptor extends HandlerInterceptorAdapter {

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView mv) throws Exception {	
        
        execute(request, null);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        
    }

    @Transactional(readOnly = true, propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
    private void execute(HttpServletRequest request, ModelAndView mv) {
        HttpSession httpSession = request.getSession(false);
        if (httpSession != null) {
            Object principal = request.getSession(false).getAttribute("name");
            if (principal != null) {
                try {
                } catch (Exception e) {
                    request.getSession(false).setAttribute("name", null);
                }
            } else {
                request.getSession(false).setAttribute("name", null);
            }
        }
    }

    @Transactional(readOnly = true, propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
    public synchronized static void storeDataInContext(HttpServletRequest context, String sessionid, String userroles) {

        Hashtable shareddata = (Hashtable)context.getAttribute("shared_userroles");

        if (shareddata==null) {
            shareddata = new Hashtable();
        }

        shareddata.put(sessionid, userroles);

        context.setAttribute("shared_userroles", shareddata);
    }

}